﻿using Microsoft.Extensions.DependencyInjection;
using System.Configuration;
using System.Data;
using System.IO;
using System.Reflection;
using System.Windows;

namespace ZeroBug.PersonalityEvaluation
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public new static App Current => (App)Application.Current;
        public IServiceProvider Services { get; }
        public App()
        {
            Services = ConfigureServices();
        }
        private static IServiceProvider ConfigureServices()
        {
            var services = new ServiceCollection();

            services.AddSingleton<IDatabase, SQLiteDatabaseHelper>();

            services.AddSingleton<RevisePasswordViewModel>();
            services.AddSingleton<RevisePasswordView>(sp => new RevisePasswordView() { DataContext = sp.GetService<RevisePasswordViewModel>() });

            services.AddSingleton<EvaluationViewModel>();
            services.AddSingleton<EvaluationView>(sp => new EvaluationView() { DataContext = sp.GetService<EvaluationViewModel>() });

            services.AddSingleton<DataStatisticsViewModel>();
            services.AddSingleton<DataStatisticsView>(sp => new DataStatisticsView() { DataContext = sp.GetService<DataStatisticsViewModel>() });

            services.AddSingleton<LoginViewModel>();
            services.AddSingleton<LoginView>(sp => new LoginView() { DataContext = sp.GetService<LoginViewModel>() });

            services.AddSingleton<MainWindowViewModel>();
            services.AddSingleton<MainWindow>(sp => new MainWindow() { DataContext = sp.GetService<MainWindowViewModel>() });

            return services.BuildServiceProvider();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            string s = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            SQLiteDatabaseHelper sq = Services.GetService<IDatabase>() as SQLiteDatabaseHelper;
            sq.SetDatabasePath(s + "\\data.db");

            var mainWindow = Services.GetService<LoginView>();
            int hs = mainWindow.GetHashCode();
            mainWindow!.ShowDialog();
        }
    }

}
